package com.medusa.aps.business.modules.modeling.controller;


import com.medusa.aps.business.model.pagination.BasePage;
import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.output.MaterialChooseVO;
import com.medusa.aps.business.modules.modeling.model.input.ChooseMaterialRouteParam;
import com.medusa.aps.business.modules.modeling.model.input.ProcessDetailParam;
import com.medusa.aps.business.modules.modeling.model.input.ProcessRouteDTO;
import com.medusa.aps.business.modules.modeling.model.input.ProcessRouteParam;
import com.medusa.aps.business.modules.modeling.model.output.OrderTechnologicalProcessVO;
import com.medusa.aps.business.modules.modeling.model.output.ProcessRouteDetailVO;
import com.medusa.aps.business.modules.modeling.model.output.ProcessRouteVO;
import com.medusa.aps.business.modules.modeling.mp.service.ProcessRouteService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工艺路线控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:48
 */
@Validated
@RestController
@RequestMapping("/process/route")
@Tag(name = "工艺路线控制层", description = "工艺路线控制层")
public class ProcessRouteController {
    /**
     * 服务对象
     */
    @Resource
    private ProcessRouteService processRouteService;

    /**
     * 分页查询所有数据
     *
     * @param processRouteParam 查询实体
     * @return 所有数据
     */
    @GetMapping("/select/page")
    @Operation(description = "分页查询所有数据", summary = "分页查询所有数据")
    public Result<Page<ProcessRouteVO>> selectPage(@Parameter(description = "查询参数") ProcessRouteParam processRouteParam) {
        return Result.ok(processRouteService.selectPage(new Page<>(processRouteParam.getCurrent(), processRouteParam.getSize()), processRouteParam));
    }

    /**
     * 查询工艺路线详情
     *
     * @param id 主键
     */
    @GetMapping("/detail")
    @Operation(description = "查询工艺路线详情", summary = "查询工艺路线详情")
    public Result<ProcessRouteDetailVO> detail(@RequestParam @Parameter(description = "主键Id") Integer id) {
        return Result.ok(processRouteService.detail(new ProcessDetailParam().setProcessId(id)));
    }

    @GetMapping("/detail/change")
    @Operation(description = "查询工序变化之后的工艺路线详情", summary = "查询工序变化之后的工艺路线详情")
    public Result<ProcessRouteDetailVO> detailChange(@RequestParam @Parameter(description = "主键Id") Integer id) {
        return Result.ok(processRouteService.detailChange(id));
    }

    /**
     * 需求订单查询工艺路线详情
     *
     * @param materialCode 物料编码
     */
    @GetMapping("/order/technological/process")
    @Operation(description = "需求订单查询工艺路线详情", summary = "需求订单查询工艺路线详情")
    public Result<Page<OrderTechnologicalProcessVO>> orderTechnologicalProcess(BasePage page, @Parameter(description = "物料编码", required = true) @Valid @NotNull(message = "物料编码不能为空") String materialCode) {
        return Result.ok(processRouteService.orderTechnologicalProcess(new Page<>(page.getCurrent(), page.getSize()), materialCode));
    }

    @GetMapping("/choose/material")
    @Operation(description = "工艺路线选择物料", summary = "工艺路线选择物料")
    public Result<Page<MaterialChooseVO>> chooseMaterial(ChooseMaterialRouteParam chooseMaterialRouteParam) {
        return Result.ok(processRouteService.chooseMaterial(chooseMaterialRouteParam));
    }

    @PostMapping("/save")
    @Operation(description = "新增数据", summary = "新增数据")
    public Result<Void> save(@RequestBody @Validated ProcessRouteDTO processRouteDTO) {
        processRouteService.saveProcessRoute(processRouteDTO);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param processRouteDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(description = "修改数据", summary = "修改数据")
    public Result<Void> updateProcessRoute(@RequestBody @Validated(Update.class) ProcessRouteDTO processRouteDTO) {
        processRouteService.updateProcessRoute(processRouteDTO);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param processRouteIds 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(description = "删除数据", summary = "删除数据")
    public Result<Void> delete(@RequestBody @Size(min = 1, message = "请选择需要删除的工艺路线") List<Integer> processRouteIds) {
        processRouteService.deleteProcessRouteByIds(processRouteIds);
        return Result.ok();
    }
}

